package rn.automacao.persistencia;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;

import rn.automacao.banco.Conexao;
import rn.automacao.dominio.FuncionarioVO;
import rn.automacao.dominio.TipoVO;

public class FuncionarioDAO implements ObjetoDAO {

	Connection conn = null;

	PreparedStatement ps = null;
	Statement stmt = null;

	ResultSet rs = null;

	public void alterar(Object objeto) {

		/*
		conn = Conexao.getInstance().abreConexao();
		try {
			ps = conn.prepareStatement("UPDATE FUNCIONARIO SET NOME=?, FUNCAO=?, SENHA=? WHERE CODIGO=?");
			ps.setString(1, ((FuncionarioVO) objeto).getNome());
			ps.setString(2, ((FuncionarioVO) objeto).getFuncao().getDescricao());
			ps.setString(3, ((FuncionarioVO)objeto).getSenha());
			ps.setInt(4, ((FuncionarioVO) objeto).getCodigo());
			ps.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}*/
	}

	public void cadastrar(Object objeto) {

		/*
		conn = Conexao.getInstance().abreConexao();
		try {
			ps = conn.prepareStatement("INSERT INTO FUNCIONARIO VALUES (?,?,?,?)");
			ps.setInt(1, ((FuncionarioVO) objeto).getCodigo());
			ps.setString(2, ((FuncionarioVO) objeto).getNome());
			ps.setString(3, ((FuncionarioVO) objeto).getFuncao().getDescricao());
			ps.setString(4, ((FuncionarioVO)objeto).getSenha());
			ps.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		*/
	}

	public void deletar(Object objeto) {

		/*
		conn = Conexao.getInstance().abreConexao();
		try {
			ps = conn.prepareStatement("DELETE FROM FUNCIONARIO WHERE CODIGO=?");
			ps.setInt(1, ((FuncionarioVO) objeto).getCodigo());
			ps.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		*/
	}

	public Collection<Object> listar() {


		Collection<Object> funcionarios = new ArrayList<Object>();
		conn = Conexao.getInstance().abreConexao();
		try {
			ps = conn.prepareStatement("SELECT * FROM FUNCIONARIO");
			rs = ps.executeQuery();

			while (rs.next()) {
				funcionarios.add(new FuncionarioVO(rs.getInt(1), ((TipoVO)new TipoDAO().pesquisarTipoFuncionario(rs.getInt(2), "tipofunc")), rs.getString(3), Integer.parseInt(rs.getString(4)), rs.getDouble(5), rs.getDouble(6)));
			}
				return funcionarios;

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public Collection<Object> pesquisar(String param, String filtro) {

		Collection<Object> funcionarios = new ArrayList<Object>();
		conn = Conexao.getInstance().abreConexao();
		try {
			ps = conn.prepareStatement("SELECT * FROM FUNCIONARIO WHERE "+filtro+" LIKE '%"+param+"%'");
			rs = ps.executeQuery();

			while (rs.next()) {
				//funcionarios.add(new FuncionarioVO(rs.getInt(1), rs
						//.getString(2), ((FuncaoVO) new FuncaoDAO().pesquisar(rs
						//.getInt(3))), rs.getString(4)));
			}
			return funcionarios;

		} catch (SQLException e) {
			e.printStackTrace();
		}

		return null;
	}

	public Object pesquisar(int param, String filtro) {

		conn = Conexao.getInstance().abreConexao();
		try {
			ps = conn.prepareStatement("SELECT * FROM FUNCIONARIO WHERE "+filtro+" = "+param);
			rs = ps.executeQuery();

			rs.next();
			return new FuncionarioVO(rs.getInt(1), ((TipoVO)new TipoDAO().pesquisarTipoFuncionario(rs.getInt(2), "tipofunc")), rs.getString(3), Integer.parseInt(rs.getString(4)), rs.getDouble(5), rs.getDouble(6));

		} catch (SQLException e) {
			e.printStackTrace();
		}

		return null;
	}

	public Object pesquisarLogin(String login, String senha) {

		System.out.println("Estou aqui!");
		conn = Conexao.getInstance().abreConexao();
		try {
			ps = conn.prepareStatement("SELECT * FROM FUNCIONARIO WHERE LOGIN=? AND SENHA=?");
			ps.setString(1, login);
			ps.setString(2, senha);
			rs = ps.executeQuery();

			rs.next();
			//return new FuncionarioVO(rs.getInt(1), rs.getString(2),
					//((FuncaoVO) new FuncaoDAO().pesquisar(rs.getInt(3))),
					//rs.getString(4));

		} catch (SQLException e) {
			e.printStackTrace();
		}

		return null;
	}
}
